<?php

namespace easydis\APSBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class SalarieType extends AbstractType
{   
    private $idexploit;
    
    public function __construct($idexploit) {
        $this->idexploit = $idexploit;
    }
    
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $opt = $this->idexploit;
        $builder
            ->add('matricule', 'text')
            ->add('nom', 'text')
            ->add('prenom', 'text')
            ->add('prenom2', 'text', array('required' => false))
            ->add('prenom3', 'text', array('required' => false))
            ->add('contrat', new ContratType());
        $builder->add('section', 'entity', array('class' => 'easydisAPSBundle:Section','property' => 'lib', 
                    'query_builder' => function($repository) use (&$opt){
                    return $repository->createQueryBuilder('sc')
                            ->leftjoin('sc.service', 'se')
                            ->join('se.groupe_services', 'gs')
                            ->join('gs.exploit', 'ex')
                            ->where('ex.id=:idex')
                            ->setParameter('idex',$opt)
                            ->orderby('gs.lib')
                            ->addorderby('se.lib')
                            ->addorderby('sc.id');
                    
                    }));
            
    }
    
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'easydis\APSBundle\Entity\Salarie'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'easydis_apsbundle_salarie';
    }
}
